package com.turing.post.capitalaccounts.impl;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFDateUtil;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import com.turing.post.capitalaccounts.util.BankAccount;
import com.turing.post.capitalaccounts.util.BankAccountDao;
import com.turing.post.capitalaccounts.util.BankAccountDetail;
import com.turing.post.capitalaccounts.util.BankAccountDetailBusiness;
import com.turing.post.capitalaccounts.util.BankAccountDetailDao;
import com.turing.post.capitalaccounts.util.CapitalApplicationException;
import com.turing.post.capitalaccounts.util.CapitalRespCode;
import com.turing.post.common.util.PCPSCommonDao;
import com.turing.post.common.util.SysOpUser;
import com.turing.post.common.util.SysOpUserDao;
import com.turing.post.common.util.SysOperLog;
import com.turing.post.common.util.SysOperLogDao;
import com.turing.post.organization.util.IssueCompany;
import com.turing.post.organization.util.IssueCompanyDao;
import com.turing.post.organization.util.SaleBranch;
import com.turing.post.organization.util.SaleBranchDao;
import com.turing.post.report.util.BinaryFileInfo;
import com.turing.post.report.util.BinaryFileInfoDao;
import com.turing.post.salesmanage.util.SalesManageApplicationException;
import com.turing.post.salesmanage.util.SalesManageRespCode;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;
import com.turing.post.util.PCPSWebInput;

/**
 * 银行帐户入账流水business实现
 * 
 * @author liun
 * @date 2013-05-29
 */
@Stateless(mappedName = "capitalaccounts.impl.BankAccountDetailBusinessImpl")
@Remote
@Local
public class BankAccountDetailBusinessImpl implements BankAccountDetailBusiness {

	@EJB(mappedName = "capitalaccounts.impl.BankAccountDetailDaoImpl")
	private BankAccountDetailDao bankAccountDetailDaoImpl;

	@EJB(mappedName = "common.impl.SysOpUserDaoImpl")
	private SysOpUserDao sysOpUserDaoImpl;

	@EJB(mappedName = "common.impl.PCPSCommonDaoImpl")
	private PCPSCommonDao pcpsCommonDaoImpl;

	@EJB(mappedName = "capitalaccounts.impl.BankAccountDaoImpl")
	private BankAccountDao bankAccountDaoImpl;

	@EJB(mappedName = "common.impl.SysOperLogDaoImpl")
	private SysOperLogDao sysOperLogDaoImpl;

	@EJB(mappedName = "report.impl.BinaryFileInfoDaoImpl")
	private BinaryFileInfoDao binaryFileInfoDaoImpl;

	@EJB(mappedName = "capitalaccounts.impl.BankAccountDaoImpl")
	private BankAccountDao bankAccountDao;
	
	@EJB(mappedName = "organization.impl.SaleBranchDaoImpl")
	private SaleBranchDao saleBranchDao;
	
	@EJB(mappedName = "organization.impl.IssueCompanyDaoImpl")
	private IssueCompanyDao issueCompanyDao;
	

	/**
	 * 异常抛出方法
	 * 
	 * @param error
	 * @throws PCPSApplicationException
	 */
	private void errException(String error) throws PCPSApplicationException {
		throw new SalesManageApplicationException(
				SalesManageApplicationException.ERRCODE_CATEGORY_APPLICATION,
				SalesManageRespCode.SALESBUSINESS_ARGS_NULL, error);
	}

	/**
	 * 审核销售机构现金上缴
	 * 
	 * @param webInput
	 * @param saleCashWithdrawBill
	 * @throws PCPSApplicationException
	 */
	@Override
	public void authBankAccountDetail(PCPSWebInput webInput,
			BigInteger importFileId) throws PCPSApplicationException {
		if (webInput == null || webInput.getOperId() == null
				|| PCPSUtil.isEmpty(webInput.getOperBranchNo())
				|| importFileId == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "审核参数为空!");
		}

		List<BankAccountDetail> list = readBankAccountDetailExcel(importFileId);
		if (list == null || list.size() <= 0) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "未找到入账流水信息!");
		}

		for (BankAccountDetail bad : list) {
			// 判断参数
			if (bad.getBankAccountId() == null) {
				throw new CapitalApplicationException(
						CapitalApplicationException.ERRMSG_NULLPOINTER,
						CapitalRespCode.RC_ARGS_NULL, "银行账户为空!");
			}
			if (PCPSUtil.isEmpty(bad.getBankAccSeqNo())) {
				throw new CapitalApplicationException(
						CapitalApplicationException.ERRMSG_NULLPOINTER,
						CapitalRespCode.RC_ARGS_NULL, "银行记账流水号为空!");
			}
			if (PCPSUtil.isEmpty(bad.getVoucherNo())) {
				throw new CapitalApplicationException(
						CapitalApplicationException.ERRMSG_NULLPOINTER,
						CapitalRespCode.RC_ARGS_NULL, "凭证号为空!");
			}
			if (bad.getVoucherAmount() == null) {
				throw new CapitalApplicationException(
						CapitalApplicationException.ERRMSG_NULLPOINTER,
						CapitalRespCode.RC_ARGS_NULL, "凭证金额为空!");
			}
			// 判断银行帐号
			BankAccount ba = bankAccountDao.getBankAccountByBankAccountId(bad
					.getBankAccountId());
			if (ba == null
					|| ba.getBankAccountId() == null
					|| !BankAccount.BANKTYPE_ISSUECOMPANY.equals(ba
							.getBankType())) {
				throw new CapitalApplicationException(
						CapitalApplicationException.ERRMSG_NULLPOINTER,
						CapitalRespCode.RC_ARGS_NULL, "银行账户不存在!");
			}
			// 判断入账流水
			BankAccountDetail bankAcc = bankAccountDetailDaoImpl
					.getBankAccountDetailByVoucherAndAmount(
							bad.getBankAccountId(), bad.getVoucherNo(),
							bad.getVoucherAmount());
			if (bankAcc == null || bankAcc.getBankAccDetailId() == null) {
				throw new CapitalApplicationException(
						CapitalApplicationException.ERRMSG_NULLPOINTER,
						CapitalRespCode.RC_ARGS_NULL, "银行帐号:"
								+ ba.getAccountNo() + ",凭证号:"
								+ bad.getVoucherNo() + "的入账流水不存在或凭证金额不一致!");
			}
			if (BankAccountDetail.STATUS_AUTOCHECK.equals(bankAcc.getStauts())
					|| BankAccountDetail.STATUS_MANUALLYCHECK.equals(bankAcc.getStauts())) {
				throw new CapitalApplicationException(
						CapitalApplicationException.ERRMSG_NULLPOINTER,
						CapitalRespCode.RC_ARGS_NULL, "银行帐号:"
								+ ba.getAccountNo() + ",凭证号:"
								+ bad.getVoucherNo() + "的入账流水已审核!");
			}
			// 取当前时间
			Date curTime = pcpsCommonDaoImpl.getDatabaseSysTime();
			IssueCompany ic = issueCompanyDao
					.getIssueCompanyByIssueCompanyNo(bankAcc
							.getCreateBranchNo().substring(0, 6));
			bankAcc.setBankAccSeqNo(bad.getBankAccSeqNo());
			bankAcc.setAuthBranchNo(webInput.getOperBranchNo());
			bankAcc.setAuthOperId(webInput.getOperId());
			bankAcc.setAuthTime(curTime);
			bankAcc.setAuthTranDate(ic.getTranDate());
			bankAcc.setImportFileID(importFileId);
			bankAcc.setStauts(BankAccountDetail.STATUS_AUTOCHECK);
			bankAccountDetailDaoImpl.modifyBankAccountDetail(bankAcc);
			if (BankAccountDetail.Cash_PaidType.equals(bankAcc.getPaidType())){
				SaleBranch sb = saleBranchDao.getSaleBranchBySaleBranchNo(bankAcc.getCreateBranchNo());
				sb.setAlreadyCheckCash(sb.getAlreadyCheckCash().add(bankAcc.getVoucherAmount()));
				saleBranchDao.updateSaleBranch(sb);
				
				// 记录操作日志
				sysOperLogDaoImpl
						.insertSysOperLogInfo(webInput, SysOperLog.operType_Edit,
								"银行帐户入账流水上缴现金审核，修改销售机构已核实未激活现金余额，", "销售机构：" + sb.getSaleBranchName()
										+ ";已核实未激活现金余额：" + sb.getAlreadyCheckCash());
			}
		}
	}

	/**
	 * 根据银行账户ID获取银行账户入账流水
	 * 
	 * @param bankAccountId
	 *            银行账户ID
	 * @param page
	 *            分页号
	 * @return
	 */
	@Override
	public List<BankAccountDetail> getBankAccountDetailByAccID(
			PCPSWebInput webInput, BigInteger accountNoId, String status,
			Date startDate, Date endDate, String voucherNo,
			BigInteger startAmount, BigInteger endAmount,
			BigInteger createBranchNo, String isSingle, String paidType, String lenghtSection, Integer page)
			throws CapitalApplicationException {
		if (accountNoId != null) {
			BankAccount account = bankAccountDaoImpl
					.getBankAccountByBankAccountId(accountNoId);
			if (account == null || PCPSUtil.isEmpty(account.getAccountNo())) {
				throw new CapitalApplicationException(
						CapitalApplicationException.ERRMSG_NULLPOINTER,
						CapitalRespCode.RC_ARGS_NULL, "帐号不存在！");
			}
		}
		return bankAccountDetailDaoImpl.getBankAccountDetailByAccID(webInput,
				accountNoId, status, startDate, endDate, voucherNo,
				startAmount, endAmount, createBranchNo, isSingle, paidType, lenghtSection, page);
	}

	/**
	 * 根据id获取银行账户入账流水
	 * 
	 * @param bankAccDetailId
	 *            银行账户入账流水ID
	 * @return
	 */
	@Override
	public BankAccountDetail getBankAccountDetailByID(BigInteger bankAccDetailId) {
		return bankAccountDetailDaoImpl
				.getBankAccountDetailByID(bankAccDetailId);
	}

	/**
	 * 获取银行账户入账流水总数
	 * 
	 * @param bankAccountId
	 *            银行账户ID
	 * @return
	 */
	@Override
	public Integer getBankAccountDetailByAccIDTotal(PCPSWebInput webInput,
			BigInteger accountNoId, String status, Date startDate,
			Date endDate, String voucherNo, BigInteger startAmount,
			BigInteger endAmount, BigInteger createBranchNo, String isSingle, String paidType, String lenghtSection)
			throws CapitalApplicationException {
		if (accountNoId != null) {
			BankAccount account = bankAccountDaoImpl
					.getBankAccountByBankAccountId(accountNoId);
			if (account == null || PCPSUtil.isEmpty(account.getAccountNo())) {
				throw new CapitalApplicationException(
						CapitalApplicationException.ERRMSG_NULLPOINTER,
						CapitalRespCode.RC_ARGS_NULL, "帐号不存在！");
			}
		}
		return bankAccountDetailDaoImpl.getBankAccountDetailByAccIDTotal(
				webInput, accountNoId, status, startDate, endDate,
				voucherNo, startAmount, endAmount, createBranchNo, isSingle, paidType, lenghtSection);
	}

	/**
	 * 添加一条银行账户入账流水明细
	 * 
	 * @param entity
	 */
	@Override
	public void addBankAccountDetail(PCPSWebInput webInput,
			BankAccountDetail entity) throws PCPSApplicationException {
		if (webInput == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "当前用户未登录！");
		}
		// 登录用户检查
		SysOpUser user = sysOpUserDaoImpl.getSysOpUserByOperId(webInput
				.getOperId());
		if (user == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.OPER_NULL, "当前用户不存在！");
		}
		// 登录用户所在机构检查
		String operBranchNO = webInput.getOperBranchNo();
		if (operBranchNO == null || operBranchNO.trim().equals("")) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "当前用户所在机构不存在！");
		}
		// 银行账户检查
		if (entity.getBankAccountId() == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "银行账户号为空！");
		}
		BankAccount account = bankAccountDaoImpl
				.getBankAccountByBankAccountId(entity.getBankAccountId());
		if (account == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "银行账户不存在！");
		}
		// 凭证号检查
		if (PCPSUtil.isEmpty(entity.getVoucherNo())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "凭证号为空！");
		}
		// 凭证金额检查
		if (entity.getVoucherAmount() == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "凭证金额为空！");
		}
		if (entity.getVoucherAmount().compareTo(BigInteger.ZERO) < 0) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.AMOUNT_SMALL, "凭证金额必须大于或等于零！");
		}

		BankAccountDetail otherObj = bankAccountDetailDaoImpl.getBankAccountDetailByUNI(entity.getVoucherNo(), entity.getPaidBankAccountId(), BankAccountDetail.STATUS_NOCHECK);
		if(otherObj != null && !otherObj.getBankAccDetailId().equals(entity.getBankAccDetailId())){
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "您录入的付款人银行账户对应的凭证号已存在！");
		}

		BankAccountDetail obj = entity;

		obj.setBankAccDetailId(pcpsCommonDaoImpl.getUniqueIntegerKey());
		obj.setCreateBranchNo(operBranchNO);
		obj.setOperId(user.getOperId());
		obj.setCreateTime(pcpsCommonDaoImpl.getDatabaseSysTime());
		obj.setDebitAmount(BigInteger.ZERO);
		// 添加时状态为未核对
		obj.setStauts(BankAccountDetail.Status_Input);
		obj.setPaidType(BankAccountDetail.Tran_PaidType);
		
		bankAccountDetailDaoImpl.addBankAccountDetail(obj);

		// 记录操作日志
		sysOperLogDaoImpl
				.insertSysOperLogInfo(webInput, SysOperLog.operType_Add,
						"添加银行账户入账流水明细", "银行帐号：" + obj.getBankAccountId()
								+ "；凭证号：" + obj.getVoucherNo());
	}

	/**
	 * 根据ID删除一条银行账户入账流水明细
	 * 
	 * @param entity
	 */
	@Override
	public void delBankAccountDetail(PCPSWebInput webInput,
			BankAccountDetail entity) throws PCPSApplicationException {
		if (webInput == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "当前用户未登录！");
		}
		// 登录用户检查
		SysOpUser user = sysOpUserDaoImpl.getSysOpUserByOperId(webInput
				.getOperId());
		if (user == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.OPER_NULL, "当前用户不存在！");
		}
		// 登录用户所在机构检查
		String operBranchNO = webInput.getOperBranchNo();
		if (operBranchNO == null || operBranchNO.trim().equals("")) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "当前用户所在机构不存在！");
		}
		// id检查
		if (entity.getBankAccDetailId() == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "银行账户入账流水明细ID为空！");
		}
		BankAccountDetail obj = bankAccountDetailDaoImpl
				.getBankAccountDetailForUpdate(entity.getBankAccDetailId());
		bankAccountDetailDaoImpl.delBankAccountDetail(obj);
		// 记录操作日志
		sysOperLogDaoImpl
				.insertSysOperLogInfo(webInput, SysOperLog.operType_Delete,
						"删除银行账户入账流水明细", "银行帐号：" + obj.getBankAccountId()
								+ "；凭证号：" + obj.getVoucherNo());
	}

	/**
	 * 修改一条银行账户入账流水明细
	 * 
	 * @param entity
	 * @throws PCPSApplicationException
	 */
	@Override
	public void modifyBankAccountDetail(PCPSWebInput webInput,
			BankAccountDetail entity) throws PCPSApplicationException {
		if (webInput == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "当前用户未登录！");
		}
		// 登录用户检查
		SysOpUser user = sysOpUserDaoImpl.getSysOpUserByOperId(webInput
				.getOperId());
		if (user == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.OPER_NULL, "当前用户不存在！");
		}
		// 登录用户所在机构检查
		String operBranchNO = webInput.getOperBranchNo();
		if (operBranchNO == null || operBranchNO.trim().equals("")) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "当前用户所在机构不存在！");
		}
		// 银行账户检查
		if (entity.getBankAccountId() == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "银行账户号为空！");
		}
		BankAccount account = bankAccountDaoImpl
				.getBankAccountByBankAccountId(entity.getBankAccountId());
		if (account == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "银行账户不存在！");
		}
		// 凭证号检查
		if (PCPSUtil.isEmpty(entity.getVoucherNo())) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "凭证号为空！");
		}
		// 凭证金额检查
		if (entity.getVoucherAmount() == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.AMOUNT_SMALL, "凭证金额为空！");
		}
		if (entity.getVoucherAmount().compareTo(BigInteger.ZERO) < 0) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "凭证金额必须大于或等于零！");
		}
		

		BankAccountDetail otherObj = bankAccountDetailDaoImpl.getBankAccountDetailByUNI(entity.getVoucherNo(), entity.getPaidBankAccountId(), BankAccountDetail.STATUS_NOCHECK);
		if(otherObj != null && !otherObj.getBankAccDetailId().equals(entity.getBankAccDetailId())){
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "您录入的付款人银行账户对应的凭证号已存在！");
		}

		BankAccountDetail obj = bankAccountDetailDaoImpl
				.getBankAccountDetailForUpdate(entity.getBankAccDetailId());
		// 检查银行账户入账流水明细
		if (obj == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "要修改的银行账户入账流水明细信息不存在！");
		}

		obj.setBankAccountId(entity.getBankAccountId());
		obj.setBankAccSeqNo(entity.getBankAccSeqNo());
		obj.setAuthBranchNo(entity.getAuthBranchNo());
		obj.setAuthOperId(entity.getAuthOperId());
		obj.setAuthTime(entity.getAuthTime());
		obj.setImportFileID(entity.getImportFileID());
		obj.setVoucherNo(entity.getVoucherNo());
		obj.setVoucherAmount(entity.getVoucherAmount());
		obj.setVoucherRemark(entity.getVoucherRemark());
		obj.setVoucherImageId(entity.getVoucherImageId());
		obj.setPaidBankAccountId(entity.getPaidBankAccountId());
		obj.setDebitAmount(entity.getDebitAmount());
		obj.setRemark(entity.getRemark());

		
		bankAccountDetailDaoImpl.modifyBankAccountDetail(obj);

		// 记录操作日志
		sysOperLogDaoImpl
				.insertSysOperLogInfo(webInput, SysOperLog.operType_Edit,
						"修改银行账户入账流水明细", "银行帐号：" + obj.getBankAccountId()
								+ ";凭证号：" + obj.getVoucherNo());
	}

	/**
	 * 解析银行转账帐户入账流水Excel
	 * 
	 * @param importFileID
	 * @return
	 * @throws PCPSApplicationException
	 */
	private List<BankAccountDetail> readBankAccountDetailExcel(
			BigInteger importFileID) throws PCPSApplicationException {
		// 判断是否为空
		BinaryFileInfo file = binaryFileInfoDaoImpl
				.getBinaryFileInfoById(importFileID);
		if (file == null || file.getFileLength() <= 0) {
			return null;
		}
		HSSFWorkbook workbook = null;
		InputStream is = null;
		try {
			byte[] xlsfile = file.getFileContent();
			byte[] aResult = new byte[xlsfile.length];
			System.arraycopy(xlsfile, 0, aResult, 0, aResult.length);
			is = new ByteArrayInputStream(aResult);
			workbook = new HSSFWorkbook(is);
		} catch (Exception e) {
			errException("入账流水信息Excel文件读取异常!");
		}
		List<BankAccountDetail> readList = new ArrayList<BankAccountDetail>();
		BankAccountDetail bad = null;
		// 读取excel
		String line = readLine(workbook);
		while (line != null) {
			bad = new BankAccountDetail();
			line = readLine(workbook);
			if (line == null)
				break;
			// 不获取第一行表头数据
			String[] arrStr = line.split("#");
			if (arrStr.length < 5) {
				errException("入账流水信息Excel文件读取格式有误!");
			}
			bad = checkarrStr(arrStr, bad);
			readList.add(bad);
		}
		// 关闭
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
				is = null;
			}
		}

		return readList;
	}

	// 当前的Sheet
	private int currSheet;
	// 当前位置
	private int currPosition;

	/**
	 * @param 读取excel中数据
	 *            通过POI提供的API读取文件 只读取第一个sheet中数据
	 * @return
	 */
	private String readLine(HSSFWorkbook workbook) throws PCPSApplicationException {
		// 第一个sheet
		HSSFSheet sheet = workbook.getSheetAt(0);
		// 判断当前行是否到但前Sheet的结尾
		if (currPosition > sheet.getLastRowNum()) {
			// 当前行位置清零,从第一行开始读取
			currPosition = 0;
			// 判断是否还有Sheet
			while (currSheet != 0) {
				// 得到下一张Sheet
				sheet = workbook.getSheetAt(currSheet + 1);
				// 当前行数是否已经到达文件末尾
				if (currPosition == sheet.getLastRowNum()) {
					// 当前Sheet指向下一张Sheet
					currSheet++;
					continue;
				} else {
					// 获取当前行数
					int row = currPosition;
					currPosition++;
					// 读取当前行数据
					return getLine(sheet, row);
				}
			}
			return null;
		}
		// 获取当前行数
		int row = currPosition;
		currPosition++;
		// 读取当前行数据
		return getLine(sheet, row);
	}

	// 函数getLine返回Sheet的一行数据
	private String getLine(HSSFSheet sheet, int row) {
		// 根据行数取得Sheet的一行
		HSSFRow rowline = sheet.getRow(row);
		// 创建字符创缓冲区
		StringBuffer buffer = new StringBuffer();
		// 获取当前行的列数
		int filledColumns = rowline.getLastCellNum();
		HSSFCell cell = null;
		// 循环遍历所有列
		for (int i = 0; i < filledColumns; i++) {
			// 取得当前Cell
			cell = rowline.getCell(i);
			String cellvalue = null;
			if (cell != null) {
				// 判断当前Cell的Type
				switch (cell.getCellType()) {
				// 如果当前Cell的Type为NUMERIC
				case HSSFCell.CELL_TYPE_NUMERIC: {
					// 判断当前的cell是否为Date
					if (HSSFDateUtil.isCellDateFormatted(cell)) {
						// 如果是Date类型则，取得该Cell的Date值
						// Date date = cell.getDateCellValue();
						// 把Date转换成本地格式的字符串
						cellvalue = cell.getDateCellValue().toString();
					}
					// 如果是纯数字
					else {
						// 取得当前Cell的数值
						Integer num = new Integer(
								(int) cell.getNumericCellValue());
						cellvalue = String.valueOf(num);
					}
					break;
				}
				// 如果当前Cell的Type为STRING
				case HSSFCell.CELL_TYPE_STRING:
					// 取得当前的Cell字符串
					cellvalue = cell.getStringCellValue().replaceAll("'", "''");
					break;
				// 默认的Cell值
				default:
					cellvalue = "";
				}
			} else {
				cellvalue = "";
			}
			// 在每个字段之间插入分割符
			buffer.append(cellvalue).append("#");
		}
		// 以字符串返回该行的数据
		if (buffer == null || PCPSUtil.isEmpty(buffer.toString())){
			return null;
		}
		return buffer.substring(0, buffer.length() - 1);
	}

	/**
	 * 验证解析
	 * 
	 * @param arrStr
	 * @throws PCPSApplicationException
	 */
	private BankAccountDetail checkarrStr(String[] arrStr, BankAccountDetail bad)
			throws PCPSApplicationException {
		if (PCPSUtil.isEmpty(arrStr[0])) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "银行账户为空!");
		}
		if (PCPSUtil.isEmpty(arrStr[2])) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "银行记账流水号为空!");
		}
		if (PCPSUtil.isEmpty(arrStr[3])) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "凭证号为空!");
		}
		if (PCPSUtil.isEmpty(arrStr[4])) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "凭证金额为空!");
		}

		List<BankAccount> baList = bankAccountDao
				.getBankAccountByAccountNo(arrStr[0]);
		if (baList == null || baList.size() <= 0
				|| baList.get(0).getBankAccountId() == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "银行账户:" + arrStr[0] + "不存在!");
		}

		String voucherAmount = PCPSUtil.changeY2F(arrStr[4]);
		if (PCPSUtil.isEmpty(voucherAmount) || "0".equals(voucherAmount)){
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "凭证金额:" + arrStr[4]
							+ "为空或格式错误!");
		}
		bad.setBankAccountId(baList.get(0).getBankAccountId());
		bad.setBankAccSeqNo(arrStr[2]);
		bad.setVoucherNo(arrStr[3]);
		bad.setVoucherAmount(new BigInteger(voucherAmount));
		return bad;

	}

	/**
	 * 手动审核
	 * @param webInput
	 * @param entity
	 * @throws PCPSApplicationException
	 */
	@Override
	public void manuallyAuthBankAccountDetail(PCPSWebInput webInput,
			BankAccountDetail entity)
			throws PCPSApplicationException {
		if (webInput == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "当前用户未登录！");
		}
		// 登录用户检查
		SysOpUser user = sysOpUserDaoImpl.getSysOpUserByOperId(webInput
				.getOperId());
		if (user == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.OPER_NULL, "当前用户不存在！");
		}
		// 登录用户所在机构检查
		String operBranchNO = webInput.getOperBranchNo();
		if (operBranchNO == null || operBranchNO.trim().equals("")) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "当前用户所在机构不存在！");
		}
		// 银行账户记账流水号
		if (entity.getBankAccSeqNo() == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "银行记账流水号为空！");
		}
		// 备注检查
		if (!PCPSUtil.isEmpty(entity.getRemark()) && entity.getRemark().length() > 127) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRCODE_CATEGORY_DATABASE,
					CapitalRespCode.RC_ARGS_NULL, "备注过长！");
		}

		BankAccountDetail obj = bankAccountDetailDaoImpl
				.getBankAccountDetailForUpdate(entity.getBankAccDetailId());
		// 检查银行账户入账流水明细
		if (obj == null) {
			throw new CapitalApplicationException(
					CapitalApplicationException.ERRMSG_NULLPOINTER,
					CapitalRespCode.RC_ARGS_NULL, "要修改的银行账户入账流水明细信息不存在！");
		}
		IssueCompany ic = issueCompanyDao
				.getIssueCompanyByIssueCompanyNo(obj
						.getCreateBranchNo().substring(0, 6));
		obj.setBankAccSeqNo(entity.getBankAccSeqNo());
		obj.setAuthBranchNo(webInput.getOperBranchNo());
		obj.setAuthTranDate(ic.getTranDate());
		obj.setAuthOperId(webInput.getOperId());
		obj.setAuthTime(pcpsCommonDaoImpl.getDatabaseSysTime());
		obj.setRemark(entity.getRemark());
		obj.setStauts(BankAccountDetail.STATUS_MANUALLYCHECK);
		bankAccountDetailDaoImpl.modifyBankAccountDetail(obj);
		
		// 记录操作日志
				sysOperLogDaoImpl
						.insertSysOperLogInfo(webInput, SysOperLog.operType_Edit,
								"人工审核银行账户入账流水明细", "银行帐号：" + obj.getBankAccountId()
										+ ";凭证号：" + obj.getVoucherNo());
		
		if (BankAccountDetail.Cash_PaidType.equals(obj.getPaidType())){
			SaleBranch sb = saleBranchDao.getSaleBranchBySaleBranchNo(obj.getCreateBranchNo());
			sb.setAlreadyCheckCash(sb.getAlreadyCheckCash().add(obj.getVoucherAmount()));
			saleBranchDao.updateSaleBranch(sb);
			
			// 记录操作日志
			sysOperLogDaoImpl
					.insertSysOperLogInfo(webInput, SysOperLog.operType_Edit,
							"银行帐户入账流水上缴现金审核，修改销售机构已核实未激活现金余额，", "销售机构：" + sb.getSaleBranchName()
									+ ";已核实未激活现金余额：" + sb.getAlreadyCheckCash());
		}
	}
}
